package com.isczy.educms.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isczy.educms.entity.CrmBanner;
import com.isczy.educms.entity.vo.BannerQueryVo;
import com.isczy.educms.service.CrmBannerService;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 后台banner管理接口
 * </p>
 *
 * @author testjava
 * @since 2020-03-07
 */
@Api(description = "后台banner管理 前端控制器")
@RestController
@RequestMapping("/educms/banneradmin")
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    /***
     * ***************************************
     * 功能描述:后台条件分页查询banner
     * @param current
     * @param limit
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-11 15:20】
     * @author modify:
     */
    @ApiOperation(value = "后台分页查询banner")
    @PostMapping("pageBanner/{current}/{limit}")
    public DataResult pageBanner(@ApiParam(name = "current",value = "当前页码数",required = true)@PathVariable Long current,
                                 @ApiParam(name = "limit",value = "每页显示条数",required = true)@PathVariable Long limit,
                                 @RequestBody(required = false) BannerQueryVo bannerQueryVo) {
        try {
            String title = bannerQueryVo.getTitle();
            String begin = bannerQueryVo.getBegin();
            String end = bannerQueryVo.getEnd();

            QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
            if (StrUtil.isNotEmpty(title)){
                wrapper.like("title",title);
            }
            if (StrUtil.isNotEmpty(begin)){
                wrapper.ge("gmt_create",begin);
            }
            if (StrUtil.isNotEmpty(end)){
                wrapper.le("gmt_create",end);
            }
            wrapper.orderByAsc("sort");
            //创建分页对象
            //调用方法实现，底层将查询出的数据封装到pageTeacher对象中
            Page<CrmBanner> pageTeacher = bannerService.page(new Page<>(current, limit), wrapper);

            return DataResult.builder()
                    .data("total",pageTeacher.getTotal())
                    .data("data",pageTeacher.getRecords())
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("条件分页查询讲师失败")
                    .params(ArrayUtil.toString(new Object[]{current,limit,bannerQueryVo}))
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:添加banner
     * @param crmBanner
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-11 15:21】
     * @author modify:
     */
    @ApiOperation(value = "添加banner")
    @PostMapping("addBanner")
    public DataResult addBanner(@RequestBody CrmBanner crmBanner) {
        try {
            bannerService.addBanner(crmBanner);
            return DataResult.builder().successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("后台添加banner失败")
                    .params(crmBanner)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:获取Banner
     * @param id
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-11 15:24】
     * @author modify:
     */
    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public DataResult get(@PathVariable String id) {
        try {
            return DataResult.builder()
                    .data(bannerService.getById(id))
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("后台获取Banner失败")
                    .params(id)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:修改Banner
     * @param banner
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-11 15:25】
     * @author modify:
     */
    @ApiOperation(value = "修改Banner")
    @PutMapping("updateById")
    public DataResult updateById(@RequestBody CrmBanner banner) {
        try {
            bannerService.updateBannerById(banner);
            return DataResult.builder().successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("后台修改Banner失败")
                    .params(banner)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:删除Banner
     * @param id
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-11 15:25】
     * @author modify:
     */
    @ApiOperation(value = "删除Banner")
    @DeleteMapping("deleteById/{id}")
    public DataResult remove(@PathVariable String id) {
        try {
            bannerService.removeBannerById(id);
            return DataResult.builder().successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("后台删除Banner失败")
                    .params(id)
                    .build();
        }
    }
}

